<?php

namespace App\Http\Controllers\Mini;

use App\Models\HotSearchs;
use App\Models\Activitys;
use App\Models\Ads;
use App\Models\Buys;
use App\Models\Collections;
use App\Models\Configs;
use App\Models\Footprints;
use App\Models\IntegralLogs;
use App\Models\SignUps;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SearchController extends Controller
{
    /**
     * 热门搜索
     * @route api.search.hotList
     */
    public function hotList(Request $request){
        try{
            $params = [
              'type' => intval($request->post('type',1)),
            ];
            $limit = intval($request->post('limit',6));
            $lists = HotSearchs::getList($params, ['content'], $limit);

            if($lists){
                return response()->json([
                    "code" => 1,
                    "msg" => "获取成功",
                    "data" => $lists
                ]);
            }else{
                return response()->json([
                    "code" => 0,
                    "msg" => "暂无数据",
                    "data" => []
                ]);
            }

        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 搜索
     * @route api.search.addSearch
     */
    public function addSearch(Request $request){
        try{
            $params = [
                'content' => trim($request->post('content','')),
                'count' => 1,
                'type' => intval($request->post('type',1)),
                'status' => 1
            ];
            if(empty($params['content'])){
                return response()->json([
                    "code" => 0,
                    "msg" => "请填写关键词！",
                ]);
            }
            $res = HotSearchs::insertSearch($params);

            if($res){
                return response()->json([
                    "code" => 1,
                    "msg" => "搜索词条已新增！"
                ]);
            }else{
                return response()->json([
                    "code" => 0,
                    "msg" => "系统错误！",
                ]);
            }

        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }


}